/**
 * @FileName: TariffClass.java
 * @Author
 * @Description:
 * @Date 2016年9月19日 上午11:25:23
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.model;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.util.InterfaceUtil;

@TableBind(tableName = "tariff_class")
public class TariffClass extends Model<TariffClass> implements Serializable{
    private static final Logger     logger            = LoggerFactory.getLogger(TariffClass.class);

    // @Fields serialVersionUID :

    private static final long       serialVersionUID  = 3024261507198590821L;

    public static final TariffClass dao               = new TariffClass();

    public static final String      ID                = "id";
    public static final String      NAME              = "name";
    public static final String      SORT              = "sort";
    public static final String      CREATED_BY        = "created_by";
    public static final String      CREATED_TIME      = "created_time";
    public static final String      ORG_ID            = "org_id";
    public static final String      LAST_UPDATED_TIME = "last_updated_time";
    public static final String      REMARK            = "remark";

    public static final String      ENABLED           = "enabled";
    public static final String      IS_DELETED        = "is_deleted";
    public static final String      ICON_URL          = "icon_url";

    public boolean checkUnique(int id, String col, String value){
        try{
            List<Record> result = Db.find("select id from " + Constant.Table.TARIFF_CLASS + " where " + col + "=?"
                    + " and is_deleted=0", new Object[] { value });
            if(result.size() <= 0 || result.get(0).getInt("id") == id){
                // System.out.println("checkUnique true col=" + col + ",value="
                // + value);
                return true;
            }

            // System.out.println("checkUnique false col=" + col + ",value=" +
            // value);
            return false;

        }
        catch(Exception e){
            e.printStackTrace();
            // System.out.println("checkUnique true col=" + col + ",value=" +
            // value);
            return true;
        }
    }

    /**
     * @Title: getTariffClassByActType
     * @Description:熊诗言：大类显示自己和自己以上机构的，小类显示自己的，
     * 因为有些大类是上级配置的， 小类是自己配置的 （根据活动类型获取当前机构及其子机构配置的资费大类数据）
     * @param actType：活动类型值（后台大类小类配置的活动类型下面的小类数据值）
     * @return List<Record>
     */
    public List<Record> getTariffClassByActType(String actType){
        int orgId = InterfaceUtil.getOrgId();
        // String orgSql = InterfaceUtil.getChlidOrgIdSql(orgId,
        // Constant.MAX_ORG_LEVEL, true);
        String orgSql = InterfaceUtil.getParentOrgIdSql(orgId, Constant.MAX_ORG_LEVEL);

        String sql = "SELECT tc.id, tc.name FROM tariff_class tc LEFT JOIN activity_tariff_class_relation atcr ON tc.id = atcr.tariff_class_id LEFT JOIN activity a ON atcr.activity_id = a.id WHERE a.is_deleted=0 AND a.type = ? AND tc.is_deleted=0 AND tc.enabled=1";
        if(StrKit.notBlank(orgSql)){
            sql += " AND tc.org_id IN " + orgSql;
        }

        // logger.info(sql);

        return Db.find(sql, actType);
    }

}
